//
//  ALPAppTools.m
//  AlpFM
//
//  Created by KING on 16/1/26.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPAppTools.h"
#import <AFNetworking.h>

@implementation ALPAppTools


+(void)getWithAFNurl:(NSString *)url block:(AFNBLOCK)block error:(AFNErrorBLOCK)errorblock{
    
    //  转码
//    NSString *urlEncode = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    
//    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
//    [manager GET:urlEncode parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        
//        //回调
//        block(responseObject);
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//        NSLog(@"失败==== %@",error);
//        //失败时回调
//        errorblock(error);
//    }];
     NSString *urlEncode = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [session GET:urlEncode parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        //        NSLog(@"成功");
//        NSMutableDictionary *dictionary = responseObject;
        block(responseObject);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        NSLog(@"失败");
        
    }];
    
}
+ (void)creatingDataTask:(NSString *)url parameters:(NSDictionary *)parameters successAFNBlock:(AFNBlock)block {
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    manager.securityPolicy.allowInvalidCertificates = NO;
    
    
    if (parameters) {
        
        NSURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:url parameters:parameters error:nil];
        
        
        NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (error) {
                NSLog(@"Error: %@", error);
            } else {
                
                block (responseObject);
            }
        }];
        [dataTask resume];
        
        
    }
    else {
        
        NSURLRequest *request =  [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:url parameters:parameters error:nil];
        
        NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (error) {
                NSLog(@"Error: %@", error);
            } else {
                
                block (responseObject);
            }
        }];
        [dataTask resume];
        
        
    }
    
    
}
@end
